﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;


namespace StudentWeb.BLL
{
    public class BookBO
    {

        #region 书籍信息 分页列表
        /// <summary>
        /// 书籍信息 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="wc"></param>
        /// <returns></returns>
        public IResult GetBookList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new BookDataDAO().GetBookList(pageIndex, pageSize, wc);
            return new DataResult<PagedList<dynamic>>(list);
        }
        #endregion
        #region 书籍信息 单条记录
        ///<summary>
        ///书籍信息 单条记录
        ///</summary>
        ///<param name="bookID"></param>
        ///<returns></returns>
        public IResult GetBookInfo(int bookID)
        {
            var res = new BookDataDAO().GetInfo(bookID);
            return new DataResult<BookData>(res);
        }
        #endregion

        #region 书籍信息 添加单条记录
        public bool InsertBookInfo(dynamic data)
        {
            var result = 0;
            var book = new BookData()
            {
                BookName = data.BookName,
                BookNo = data.BookNo,

            };
            result = new BookDataDAO().Insert(book);

            return result > 0;
        }
        #endregion

        #region 书籍信息 删除单条记录
        ///<summary>
        ///书籍信息 删除单条记录
        ///</summary>
        ///<param name="bookID"></param>
        ///<returns></returns>
        public IResult DeleteBookInfo(int bookID)
        {
            int res = new BookDataDAO().Delete(bookID);
            if (res > 0)
            {
                return new Result(ResultCode.Success);
            }
            return new Result(ResultCode.Fail);
        }
        #endregion

        #region 书籍信息 修改单条记录

        public bool UpdateBookInfo(dynamic data)
        {
            int bookID = data.BookID;
            var result = 0;
            var record = new BookDataDAO().GetInfo(bookID);
            record.BookName = data.BookName;
            record.BookNo = data.BookNo;
            result = new BookDataDAO().Update(record);
            return result > 0;
        }
        #endregion

        #region 书籍信息 全部记录
        ///<summary>
        ///书籍信息 全部记录
        ///</summary>
        ///<returns></returns>
        public IResult GetAllBookList()
        {
            var list = new BookDataDAO().GetBookList();
            return new DataResult(list);
        }
        #endregion

        #region 书籍信息 保存单条记录
        ///<summary>
        ///书籍信息 保存单条记录
        ///</summary>
        ///<returns></returns>
        public bool SaveBookInfo(dynamic data)
        {
            int bookID = data.BookID ?? 0;
            var result = 0;
            //var book=new BookData(); 此条可代替int bookID = data.BookID ?? 0;
            if (bookID > 0)
            {
                var record = new BookDataDAO().GetInfo(bookID);
                record.BookName = data.BookName;
                record.BookNo = data.BookNo;
                result = new BookDataDAO().Update(record);
            }
            else if (bookID == 0)
            {
                var book = new BookData()
                {
                    BookName = data.BookName,
                    BookNo = data.BookNo,

                };
                result = new BookDataDAO().Insert(book);
            }
            return result > 0;
        }
        #endregion

        
    }
}
